package com.zq.widget.inverted.game.role.equipment.weapon.gun;

import android.graphics.PointF;
import android.view.animation.AnimationUtils;

import com.zq.widget.inverted.game.role.Role;
import com.zq.widget.inverted.game.role.RoleGroup;
import com.zq.widget.inverted.game.role.level.Level;
import com.zq.widget.inverted.game.role.level.LevelProxy;

/**
 * Created by zhangqiang on 2018/4/4.
 */
public class Gun2 extends AbstractGun {

    private static final long maxCollingTime = 800;
    private static final long minCollingTime = 200;
    private long lastShootTime;
    private RoleGroup holder;
    private BulletFactory bulletFactory;
    private PointF shootPoint;

    public Gun2(RoleGroup holder, BulletFactory bulletFactory, Level levelProxy,PointF shootPoint) {
        super(levelProxy);
        this.holder = holder;
        this.bulletFactory = bulletFactory;
        this.shootPoint = shootPoint;
    }

    public Gun2(RoleGroup holder, BulletFactory bulletFactory, int maxLevel,PointF shootPoint) {
        this(holder,bulletFactory,new LevelProxy(maxLevel),shootPoint);
    }

    public Gun2(RoleGroup holder, BulletFactory bulletFactory,PointF shootPoint) {
        this(holder,bulletFactory,new LevelProxy(10),shootPoint);
    }

    @Override
    public void compute() {

        long timeMillis = AnimationUtils.currentAnimationTimeMillis();
        long collingTime = computeCoolingTime();
        if(timeMillis - lastShootTime > collingTime && bulletFactory != null){

            Role[] roles = bulletFactory.create(this);
            holder.addChild(roles);

            lastShootTime = timeMillis;
        }
    }

    private long computeCoolingTime() {

        return (long) (maxCollingTime - (maxCollingTime - minCollingTime) * levelFactor());
    }

    @Override
    public boolean isValid() {
        return true;
    }

    @Override
    public void onDrop() {

    }

    @Override
    public int type() {
        return 0;
    }


    @Override
    public int priority() {
        return 0;
    }

    @Override
    public PointF shootPoint() {
        return shootPoint;
    }
}
